Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    // 搜索
    serach_pro: "",
    prosum: null,
    prolist: [],
    loading: true,
    timer: '',
    kc_filter: '',
    fNum: '',
    total: '',
    kc_labelList: []
  },
  // 搜索
  serachProChange(e) {
    this.setData({
      prolist: [],
      serach_pro: e.detail,
    });
  },

  goOnChange(e) {
    this.setData({
      serach_pro: e.detail,
    });
  },

  goSearch() {
    this.setData({
      prolist: [],
      prosum: ''
    });
    const timer = new Date().getTime()
    console.log(timer, 'timer');
    this.setData({
      timer: timer
    })
    this.getList(timer);
  },
  toprodetail(e) {
    let params = {
      "corpCredit": e.currentTarget.dataset.id
    }
    console.log(params, 'paramsparams')
    getApp()
      .globalData.request("/map/queryKcScoreInfo", params, "POST")
      .then((res) => {
        console.log(res, 'resres')
        if (res.code == 200) {
          if (res.data) {
            wx.navigateTo({
              url: `/packageA/pages/homelink/innovation_score_detail/innovation_score_detail?corpKey=${e.currentTarget.dataset.id}&shortName=${e.currentTarget.dataset.name}`
            });
          } else {
            wx.showModal({
              title: '提示',
              content: '该企业数据不全，暂无法计算其科创评分。',
              showCancel: false,
              confirmText: '确定',
              success: function () {}
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: '请先登录后查看详情',
            showCancel: false,
            confirmText: '确定',
            success: function () {
              wx.setStorageSync("token", "cxgc123456");
              wx.navigateTo({
                url: "/pages/launch_start/launch_start",
              });
            }
          })
        }
      });
  },
  phone(event) {
    wx.makePhoneCall({
      phoneNumber: event.currentTarget.id,
    });
  },
  location(event) {
    var _this = this;
    wx.openLocation({
      latitude: _this.data.prolist[event.currentTarget.id].latitude,
      longitude: _this.data.prolist[event.currentTarget.id].longitude,
      name: _this.data.prolist[event.currentTarget.id].d2CorpName,
      address: _this.data.prolist[event.currentTarget.id].d2Dom,
      scale: 18,
    });
  },
  // 滑动底部加载更多
  scrollBottom() {
    this.onBottom();
  },
  goBack() {
    wx.navigateBack({
      delta: -1
    })
  },
  getList(timer) {
    var _this = this;
    this.setData({
      loading: true,
    })
    wx.showLoading({
      title: "加载中",
    });
    const params = {
      pageSize: 50,
      pageNum: this.data.pageNum,
      ...this.data.kc_filter
    };
    console.log(params, 'params');
    getApp()
      .globalData.request_cydn("/kcorpInfo/industry/getKCqy", params, "POST")
      .then((res) => {
        setTimeout(function () {
          wx.hideLoading();
        }, 500);
        const rstObj = res;
        console.log(res, "res");
        if (timer === this.data.timer) {
          if (rstObj.code == 200) {
            _this.setData({
              prolist: this.data.prolist.concat(rstObj.rows),
              pageNum: ++this.data.pageNum,
              prosum: rstObj.total,
            });
            console.log(this.data.pageNum, 'this.data.pageNum');
            this.setData({
              loading: false,
            })
          }
        }
      });
  },

  onBottom() {
    if (this.data.prolist.length >= this.data.prosum) return
    const timer = new Date().getTime()
    console.log(timer, 'timer');
    this.setData({
      timer: timer
    })
    this.getList(timer);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.setData({
      kc_filter: wx.getStorageSync('kc_filter'),
      kc_labelList: wx.getStorageSync('kc_labelList')
    })
    const fNum = Object.values(this.data.kc_filter).reduce((a, b)=> {
      if(Array.isArray(b)) {
        return a + b.length
      } else {
        return a + 0
      }
    }, 0)
    this.setData({
      fNum: fNum
    })
    const timer = new Date().getTime()
    console.log(timer, 'timer');
    this.setData({
      timer: timer
    })
    this.getList(timer);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.onBottom();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});